package com.sx.pipeline.ui.territory

import android.app.Application
import android.text.TextUtils
import com.blankj.utilcode.util.ToastUtils
import com.sx.mvvmlibs.base.BaseViewModel
import com.sx.mvvmlibs.event.SingleLiveEvent
import com.sx.mvvmlibs.http.CommonObserver
import com.sx.mvvmlibs.utils.Logs
import com.sx.mvvmlibs.utils.RxThreadHelper
import com.sx.pipeline.app.model.CYBaseViewModel
import com.sx.pipeline.repos.http.req.AddAdministrativeDTO
import com.sx.pipeline.repos.http.req.DistrictBorderDTO
import com.sx.pipeline.repos.http.req.TaskDelayDTO
import com.sx.pipeline.repos.http.res.Ipage
import com.sx.pipeline.repos.http.res.TerritoryVO

/**
 * @Author: YY
 * @Date: 2022/8/3
 * @Desc: TODO
 */
class TerritoryVM(app: Application): CYBaseViewModel(app) {
    var currentType=0;
    /**
     * 获取 Tab标题
     */
    fun getTerritoryTypeList(): List<String> {
        val tabs = mutableListOf<String>()
        tabs.add("绘制")
        tabs.add("KML文件")
        tabs.add("DXF文件")
        tabs.add("行政区域")
        return tabs
    }

    /**
     * 获取区域列表
     */
    val territoryListEvent: SingleLiveEvent<Ipage<TerritoryVO>> = SingleLiveEvent()

    fun getTerritoryList(type: Int, page: Int) {
        currentType=type
        addSubscribe(mModel.getTerritoryList(type, page, limit = 999)
            .compose(RxThreadHelper.rxSchedulerHelper())
            .subscribeWith(object : CommonObserver<Ipage<TerritoryVO>, BaseViewModel<*>>
                (this, true) {

                override fun onResult(result: Ipage<TerritoryVO>) {
                    Logs.d("getTerritoryList ${result.records.size}")
                    territoryListEvent.postValue(result)
                    if (page == 1) {
                        mUI.initRefreshEvent().postValue(true)
                    } else {
                        mUI.initLoadEvent().postValue(true)
                    }
                }

                override fun onFailed(code: Int, msg: String?) {
                    super.onFailed(code, msg)
                    if (page == 1) {
                        mUI.initRefreshEvent().postValue(false)
                    } else {
                        mUI.initLoadEvent().postValue(false)
                    }
                }
            })
        )
    }

    /**
     * 删除文件
     */
    val deleteTerritoryEvent: SingleLiveEvent<Void> = SingleLiveEvent()

    val autoRefreshEvent = SingleLiveEvent<Boolean>()

    fun deleteTerritory(ids: Long) {
        addSubscribe(mModel.deleteTerritory(ids)
            .compose(RxThreadHelper.rxSchedulerHelper())
            .subscribeWith(object : CommonObserver<Any, BaseViewModel<*>>
                (this, true) {

                override fun onResult(result: Any) {
                    ToastUtils.showShort(result.toString())
                    autoRefreshEvent.call()
                }

                override fun onFailed(code: Int, msg: String?) {
                    super.onFailed(code, msg)
                }
            })
        )
    }

    fun deleteDistrictBorder(ids: Long?) {
        addSubscribe(mModel.deleteDistrictBorder(ids!!)
            .compose(RxThreadHelper.rxSchedulerHelper())
            .subscribeWith(object : CommonObserver<Any, BaseViewModel<*>>
                (this, true) {

                override fun onResult(result: Any) {
                    ToastUtils.showShort(result.toString())
                    deleteTerritoryEvent.call()
                }

                override fun onFailed(code: Int, msg: String?) {
                    super.onFailed(code, msg)
                }
            })
        )
    }


    /**
     * 获取行政区域
     */
    val districtBorderListEvent: SingleLiveEvent<Ipage<DistrictBorderDTO>> = SingleLiveEvent()

    fun getDistrictBorderList(page: Int) {
        addSubscribe(mModel.getDistrictBorderList(page, limit = 999)
            .compose(RxThreadHelper.rxSchedulerHelper())
            .subscribeWith(object : CommonObserver<Ipage<DistrictBorderDTO>, BaseViewModel<*>>
                (this, true) {

                override fun onResult(result: Ipage<DistrictBorderDTO>) {
                    Logs.d("getTerritoryList ${result.records.size}")
                    districtBorderListEvent.postValue(result)
                    if (page == 1) {
                        mUI.initRefreshEvent().postValue(true)
                    } else {
                        mUI.initLoadEvent().postValue(true)
                    }
                }

                override fun onFailed(code: Int, msg: String?) {
                    super.onFailed(code, msg)
                    if (page == 1) {
                        mUI.initRefreshEvent().postValue(false)
                    } else {
                        mUI.initLoadEvent().postValue(false)
                    }
                }
            })
        )
    }


    /**
     * 添加行政区域负责区域
     */
    fun addAdministrative(proId:Int ,cityId:Int ,areaId:Int ,name:String) {
        var dto= AddAdministrativeDTO(proId,cityId,areaId, name)
        addSubscribe(
            mModel.addAdministrative(dto)
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<Any, BaseViewModel<*>>
                    (this, true) {
                    override fun onResult(result: Any) {
                        ToastUtils.showShort("添加成功")
                        getDistrictBorderList(page=1)
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        ToastUtils.showShort(msg)
                    }
                })

        )
    }
}